package cn.gson.crm.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gson.crm.model.dao.LLJ_ComplainDao;
import cn.gson.crm.model.dao.LLJ_ComplainTypeDao;
import cn.gson.crm.model.dao.LLJ_CustomerinfoDao;
import cn.gson.crm.model.dao.LLJ_TimeDealDao;
import cn.gson.crm.model.pojo.Complain;
import cn.gson.crm.model.pojo.ComplainType;
import cn.gson.crm.model.pojo.Customerinfo;
import cn.gson.crm.model.pojo.TimeDeal;
import cn.gson.crm.model.pojoVo.LLJ_ComplainNeedDataVo;
import cn.gson.crm.model.pojoVo.LLJ_ComplainTypeVo;
import cn.gson.crm.model.pojoVo.LLJ_ComplainVo;
import cn.gson.crm.model.pojoVo.LLJ_TimeDealVo; 

@Service
@Transactional(rollbackFor = Exception.class)
public class LLJ_ComplainService { 
	@Autowired
	private LLJ_ComplainDao llj_ComplainDao;
	@Autowired
	private LLJ_ComplainTypeDao llj_ComplainTypeDao;
	@Autowired
	private LLJ_TimeDealDao llj_TimeDealDao;
	@Autowired
	private LLJ_CustomerinfoDao llj_CustomerinfoDao;
	
	
	public long count(){  
		return llj_ComplainDao.count();
	}
	 
	public List<LLJ_ComplainVo> getAllComplainVoPages(int page,int rows){
		PageRequest p=new PageRequest(page-1, rows);  
		List<Complain> listComplain=llj_ComplainDao.findByPage(p);
		
		List<LLJ_ComplainVo> listComplainVo=new ArrayList<>();      
		listComplainVo.addAll(getAllComplainVoPages());  
		return listComplainVo;                 
		
	}
	
	/**
	 * 分页多表查询
	 */
	public List<LLJ_ComplainVo> getAllComplainVoPages(){
		
		List<Complain> listComplain=(List<Complain>) llj_ComplainDao.findAll();
		int listSize=listComplain.size();  
		
		List<LLJ_ComplainVo> LLJ_ComplainVoList=new ArrayList<LLJ_ComplainVo>();
		for(Complain cp:listComplain){
			LLJ_ComplainVo llj_ComplainVo2=new LLJ_ComplainVo(cp.getComId(), cp.getComDatetime(), 
					cp.getComDealProcess(), cp.getComDealResult(), 
					cp.getComDescribe(), cp.getComFeedback(), 
					cp.getComFistReceiverId(), cp.getComFistReceiverName(), 
					cp.getComPeople(), cp.getComRemarks(), 
					cp.getComSure(), cp.getComTheme(), 
					cp.getComUrgency(), cp.getCustomerId(), cp.getCustomerName());  
			
			ComplainType complainType=cp.getComplainType();
			TimeDeal llj_TimeDealVo=cp.getTimeDeal();
			if(complainType!=null){
				LLJ_ComplainTypeVo llj_ComplainTypeVo=new LLJ_ComplainTypeVo(complainType.getComplainId(),complainType.getComplainContain());
				llj_ComplainVo2.setComplainId(llj_ComplainTypeVo.getComplainId());
				llj_ComplainVo2.setComplainContain(llj_ComplainTypeVo.getComplainContain()); 
			}
			if(llj_TimeDealVo!=null){
				LLJ_TimeDealVo llj_TimeDealVoT=new LLJ_TimeDealVo(llj_TimeDealVo.getDealId(), llj_TimeDealVo.getDealContain());
				llj_ComplainVo2.setDealId(llj_TimeDealVoT.getDealId());
				llj_ComplainVo2.setGetDealContain(llj_TimeDealVoT.getDealContain()); 
			}
			LLJ_ComplainVoList.add(llj_ComplainVo2);
		}
		return LLJ_ComplainVoList;      
	}
	
	
	
	/**  
	 * 通过客户投诉id查询'客户投诉表'信息
	 */   
	public LLJ_ComplainVo searchComplain(Long comId){ 
		LLJ_ComplainVo llj_ComplainVo=new LLJ_ComplainVo();
		try {
			Complain complain=llj_ComplainDao.findOne(comId);  
			
			llj_ComplainVo.setComId(complain.getComId());
			llj_ComplainVo.setComTheme(complain.getComTheme());
			llj_ComplainVo.setCustomerName(complain.getCustomerName());
			llj_ComplainVo.setComDatetime(complain.getComDatetime());
			llj_ComplainVo.setComUrgency(complain.getComUrgency());
			llj_ComplainVo.setComFistReceiverId(complain.getComFistReceiverId());
			llj_ComplainVo.setComFistReceiverName(complain.getComFistReceiverName());
			llj_ComplainVo.setComDealResult(complain.getComDealResult());
			llj_ComplainVo.setComPeople(complain.getComPeople());  
			llj_ComplainVo.setComDescribe(complain.getComDescribe());
			llj_ComplainVo.setComDealProcess(complain.getComDealProcess());
			llj_ComplainVo.setComFeedback(complain.getComFeedback());
			llj_ComplainVo.setComRemarks(complain.getComRemarks());
			llj_ComplainVo.setComSure(complain.getComSure());
			llj_ComplainVo.setCustomerId(complain.getCustomerId());
			
			
			ComplainType complainType=complain.getComplainType();
			TimeDeal timeDeal=complain.getTimeDeal();
			if(complainType!=null){
				llj_ComplainVo.setComplainId(complain.getComplainType().getComplainId());
	            llj_ComplainVo.setComplainContain(complain.getComplainType().getComplainContain());
			}
			if(complainType!=null){
				llj_ComplainVo.setDealId(complain.getTimeDeal().getDealId());
				llj_ComplainVo.setGetDealContain(complain.getTimeDeal().getDealContain());
			}
			   
			return llj_ComplainVo;         
		} catch (Exception e) { 
			e.printStackTrace();
			throw new ServiceException(e);        
		}
	}   
	
	/**
	 * 添加客户投诉信息
	 */
	public boolean addComplaintMessage(LLJ_ComplainNeedDataVo NeedDataVo){  
		try {
			Complain complain=new Complain();    

			complain.setComDatetime(NeedDataVo.getComDatetime()); 
			complain.setComDealProcess(NeedDataVo.getComDealProcess());
			complain.setComDealResult(NeedDataVo.getComDealResult());
			complain.setComDescribe(NeedDataVo.getComDescribe());
			complain.setComFeedback(NeedDataVo.getComFeedback());  
			complain.setComFistReceiverName(NeedDataVo.getComFistReceiverName());
			complain.setComPeople(NeedDataVo.getComPeople());
			complain.setComRemarks(NeedDataVo.getComRemarks());
			complain.setComSure(NeedDataVo.getComSure());
			complain.setComTheme(NeedDataVo.getComTheme());
			complain.setComUrgency(NeedDataVo.getComUrgency());
			complain.setCustomerName(NeedDataVo.getCustomerName()); 
			     
			Long ComplainId=(long) Integer.parseInt(NeedDataVo.getDealContain());
			Long DealId=(long) Integer.parseInt(NeedDataVo.getDealContain());
			
			complain.setComplainType(llj_ComplainTypeDao.findOne(ComplainId));
			complain.setTimeDeal(llj_TimeDealDao.findOne(DealId));    
			llj_ComplainDao.save(complain);  
			return true;           
		} catch (Exception e) {      
			return false;      
		}        
	}         
	
	/**
	 * 编辑客户投诉信息
	 * @throws Exception 
	 */
	public String editComplain_Service(LLJ_ComplainNeedDataVo NeedDataVo) throws Exception{
		System.out.println(NeedDataVo.getComplainId()); 
		System.out.println(NeedDataVo.getComplainContain());
		      
		Complain complain=new Complain();     
		List<Complain> list=new ArrayList<>();
		String judge="failed";    
		                      
		try {
			complain.setComDatetime(NeedDataVo.getComDatetime());
			complain.setComDealProcess(NeedDataVo.getComDealProcess()); 
			complain.setComDealResult(NeedDataVo.getComDealResult());
			complain.setComDescribe(NeedDataVo.getComDescribe());
			complain.setComFeedback( NeedDataVo.getComFeedback());
			complain.setComFistReceiverName(NeedDataVo.getComFistReceiverName());
			complain.setComPeople(NeedDataVo.getComPeople());
			complain.setComRemarks(NeedDataVo.getComRemarks());
			complain.setComSure(NeedDataVo.getComSure());
			complain.setComTheme(NeedDataVo.getComTheme());
			complain.setComUrgency(NeedDataVo.getComUrgency());
			complain.setCustomerName(NeedDataVo.getCustomerName());  
			
			if(NeedDataVo.getComplainContain()!=null){
				Long ComplainId=(long) Integer.parseInt(NeedDataVo.getComplainContain());
				System.out.println("输出转换后的外键id:"+ComplainId); 
				complain.setComplainType(llj_ComplainTypeDao.findOne(ComplainId));
			}
			if(NeedDataVo.getDealContain()!=null){
				Long DealId=(long) Integer.parseInt(NeedDataVo.getDealContain());
				System.out.println("输出getComId:"+NeedDataVo.getComId()); 
				complain.setTimeDeal(llj_TimeDealDao.findOne(DealId));  
			}
			
			list.add(complain);   
			llj_ComplainDao.findOne(NeedDataVo.getComId());        
			llj_ComplainDao.save(complain);   
			judge="success"; 
		} catch (Exception e) {
			return e.getMessage();  
		}
		return judge;     
	}   
	
	/**
	 * 删除一条投诉记录
	 */
	public String deleteOneData(Long comId){
		try {
			Complain complain=llj_ComplainDao.findOne(comId);
			llj_ComplainDao.delete(complain);
			return "success"; 
		} catch (Exception e) {   
			return e.getMessage(); 
		}
	}   
	
	
	
	/**
	 * 通过投诉主题分页查询(无分页显示)
	 */
	public List<Complain> searchByTheme(String key){
		return llj_ComplainDao.queryFindAllByTheme('%'+key+'%');
	}
	
	/**
	 * 通过客户姓名查询(无分页显示)
	 */
	public List<Customerinfo> searchByName(String key){
		System.out.println("输出长度为:"+llj_CustomerinfoDao.searchAllByName('%'+key+'%').size());
		return llj_CustomerinfoDao.searchAllByName('%'+key+'%');              
	}    
	
	/**
	 * 查询客户投诉类型   
	 */
	public List<ComplainType> searchType(){  
		return llj_ComplainTypeDao.searchAllComplainType();   
	}
	
	/**
	 * 查询客户投诉事件类别
	 */
	public List<TimeDeal> searchTimeDeal(){
		return llj_TimeDealDao.searchAllTimeDeal();     
	}  
}
